<?php
namespace app\index\controller;

use \daswork\Controller;
use app\admin\model\Attenders;

Class Index extends Controller
{
    public function index()
    {
        $this->fetch();
    }

    public function save()
    {
        if ($_SERVER['REQUEST_METHOD']=='POST') {
            $param = escapeString($_POST);
            // echo json_encode($param);
            // exit;
            //查询是否已经注册过
            $model = new Attenders();
            $name = handleInput($param['name']) ? $param['name'] : '';
            $phone = handleInput($param['phone']) ? $param['phone'] : '';
            $query = "select count(*) as total from attenders where truename='$name' and phone=$phone";
            $result = $model->query($query)->fetch_assoc();
            // echo json_encode($result['total']>0);
            // exit;
            /*
            tax_type 开票类型
            company_name 公司名称
            tax_number 纳税人识别号
            company_address 公司地址
            company_phone 公司电话
            bank_account 开户行及账号
            mailing_address 邮寄地址
            postcode 邮编
            name 名字
            sex 性别
            position 职务
            phone 电话
            email 邮箱
            fee_id 注册费类型
            school 学校
            professional 专业
            paper_type '论文类型:0, 不发表; 1, << 中国腐蚀与防护学报 >> 2000元/篇; 2, << 混凝土与水泥制品 >> 3200元/篇',
            is_report 是否作报告
            report_title 报告标题
            hotel_type 酒店类型
            room_type 房间类型
            pay_type 支付方式 1, 现场缴费 2汇款
            create_time '填表时间'
            */
            if($result['total']>0){
                echo "<script>alert('您已经报名成功，请勿重复提交！');location.href='http://meeting.ecorr.org';</script>";
            }else{
                $tax_type = handleInput($param['tax_type']) ? $param['tax_type'] : 1;
                $company_name = handleInput($param['company_name']) ? $param['company_name'] : '';
                $tax_number = handleInput($param['tax_number']) ? $param['tax_number'] : '';
                $company_address = handleInput($param['company_address']) ? $param['company_address'] : '';
                $company_phone = handleInput($param['company_phone']) ? $param['company_phone'] : '';
                $bank_account = handleInput($param['bank_account']) ? $param['bank_account'] : '';
                $mailing_address = handleInput($param['mailing_address']) ? $param['mailing_address'] : '';
                $postcode = handleInput($param['postcode']) ? $param['postcode'] : '';

                $name = handleInput($param['name']) ? $param['name'] : '';
                $sex = handleInput($param['sex']) ? $param['sex'] : 0;
                $position = handleInput($param['position']) ? $param['position'] : '';
                $phone = handleInput($param['phone']) ? $param['phone'] : '';
                $email = handleInput($param['email']) ? $param['email'] : '';

                $fee_id = handleInput($param['fee_id']) ? $param['fee_id'] : 1;
                $is_report = handleInput($param['is_report']) ?$param['is_report'] : 0;
                $report_title = handleInput($param['report_title']) ? $param['report_title'] : '无报告';
                
                // $hotel_type = handleInput(isset($param['hotel_type'])?$param['hotel_type']:1);
                $hotel_type = 1;
                $room_type = handleInput($param['room_type']) ?$param['room_type'] : 1;
                $is_share = handleInput($param['is_share']) ? $param['is_share'] : 0;
                $pay_type = handleInput($param['pay_type']) ? $param['pay_type'] : 1;
                $note = handleInput($param['note']) ? $param['note'] : '';
                $create_time = time();

                $data = [
                    "meeting_id" => 3,
                    "truename" => $name,
                    "sex" => $sex,
                    "organization" => $company_name,
                    "position" => $position,
                    "phone" => $phone,
                    "email" => $email,
                    "remark" => $note,
                    "fee_type" => $fee_id,
                    "is_report" => $is_report,
                    "report_title" => $report_title,
                    "hotel_type" => $hotel_type,
                    "room_type" => $room_type,
                    "is_share" => $is_share,
                    "pay_type" => $pay_type,
                    "tax_type" => $tax_type,
                    "tax_title" => $company_name,
                    "tax_number" => $tax_number,
                    "company_address" => $company_address,
                    "company_phone" => $company_phone,
                    "bank_account" => $bank_account,
                    "mailing_address" => $mailing_address,
                    "postcode" => $postcode,
                    "create_time" => $create_time
                ];
                $res = $model->save($data);
                // var_dump($res);
                // exit;
                if($res){
                    echo "<script>alert('报名成功');location.href='http://meeting.ecorr.org';</script>"; 
                }else{
                    echo "<script>alert('失败！请重新填写');history.back();</script>";
                }
            }
        } else {
            echo "<script>alert('非法操作！');location.href='http://meeting.ecorr.org';</script>";
        }
    }
}
